/* Trace messages to debugger */
jQuery.trace = function(msg) {
  if(window.console) {
     console.log(msg);
  } else {
     alert(msg);
  }
};

/* Create new elements */
jQuery.create = function() {
	if (arguments.length == 0) return [];
	var args = arguments[0] || {}, elem=null, elements=null;
	var siblings = null;
	
	// In case someone passes in a null object, assume that they want an empty string.
	if (args == null) args = "";
	if (args.constructor == String) {
		if (arguments.length > 1) {
			var attributes = arguments[1];
			
			if (attributes.constructor == String) {
				elem = document.createTextNode(args);
				elements = [];
				elements.push(elem);
				siblings = jQuery.create.apply(null, Array.prototype.slice.call(arguments, 1));
				elements = elements.concat(siblings);
				return elements;
			} else {
				elem = document.createElement(args);

				// Set element attributes.
				var attributes = arguments[1];

				for (var attr in attributes)
					jQuery(elem).attr(attr, attributes[attr]);

				// Add children of this element.
				var children = arguments[2];
				children = jQuery.create.apply(null, children);
				jQuery(elem).append(children);

				// If there are more siblings, render those too.
				if (arguments.length > 3) {
					siblings = jQuery.create.apply(null, Array.prototype.slice.call(arguments, 3));
					return [elem].concat(siblings);
				}
				
				return elem;
			}
		} else {
			return document.createTextNode(args);
		}
	} else {
		elements = [];
		elements.push(args);
		siblings = jQuery.create.apply(null, (Array.prototype.slice.call(arguments, 1)));
		elements = elements.concat(siblings);
		return elements;
	}
};

/* Set Size */
jQuery.fn.setSize = function(w, h) {
	this.width	= w;
	this.height	= h;
	return this;
};
/* Set Position */
jQuery.fn.setPos = function(x, y) {
	this.css('top')		= y;
	this.css('left')	= x;
	return this;
};

jQuery.fn.scale = function(xScale, yScale) {
	if(yScale < 0) {
		this.width(this.width * xScale);
		this.height(this.height * xScale);
	} else {
		this.width(this.width * xScale);
		this.height(this.height * yScale);
	}
};

//Flip
jQuery.fn.flipX = function() {
	this.pixastic("fliph");
};
jQuery.fn.flipY = function() {
	this.pixastic("flipv");
};

//Behaviors
jQuery.fn.button = function() {
	this.css('cursor', 'pointer');
};

jQuery.fn.toFront = function() {
	this.css('z-index', 0);
};
jQuery.fn.toBack = function() {
	this.css('z-index', 1);
};

/* LiquidGear */
jQuery.LiquidGear = {
	stage:$(document),
	basePath:'',
	control:'XML/control.xml',
	siteXML:{},
	data:{},
	
	//Start Loading Page
	start:function() {
		//Setting Environment Type
		//this.envCheck();
		
		//this.stage.resize(this.onResize);
		
		//Load XML
		//var pageData = this.xmlData('control', this.basePath+this.control);
	},
	
	/* Get XML Data */
	xmlData:function(name, url) {
		var xmlName = 'test';
		$.ajax({async:true, cache:false, complete:this.onLoadControl, dataType:'xml', error:this.onErrorData, url:url});
	},
	onLoadControl:function(req, status) {
		$.trace('onLoadControl');
		//$.trace(req.responseXML);
		$.trace($.LiquidGear.control);
		//function(data, status){
		//	this.siteXML	= data;
	},
	onErrorData:function(req, status, error) {
		$.trace(errorThrown);
	},
		
	jsonData:function(name) {
		$.trace(name);
		var rand	= Math.floor(Math.random()*3000);
		$.get(this.xName, {bustcache:rand}, function(data, status){
			this.siteXML	= data;
		}, 'xml');
	},
	/* Events */
	ModelEvent:{
		LOADED:'data_loaded',
		ERROR:'data_error'
	},
	PageEvent:{
		OVER:'media_over',
		OUT:'media_out',
		CLICK:'media_click',
		DOWN:'media_down',
		UP:'media_up',
		COMPLETE:'page_complete',
		LOADING:'page_loading',
		SETUP:'page_setup',
		OPEN:'page_open',
		CLOSE:'page_close',
		START:'page_start',
		PRELOADED:'page_preloaded',
		LOADED:'page_loaded',
		PROGRESS:'page_progress'
	},
	MediaEvent:{
		OVER:'media_over',
		OUT:'media_out',
		CLICK:'media_click',
		DOWN:'media_down',
		UP:'media_up',
		PROGRESS:'media_progress',
		LOADED:'media_loaded',
		READY:'media_ready',
		RENDER:'media_render',
		ADDED:'media_added',
		GLOW:'media_glow',
		SCROLL_LEFT:'media_scrollLeft',
		SCROLL_RIGHT:'media_scrollRight',
		CUE:'media_cue',
		COMPLETE:'media_complete',
		FINISHED:'media_finished',
		TWEENED:'media_tweened',
		START:'media_start',
		STOP:'media_stop',
		PLAYING:'media_playing',
		CHANGE:'media_change',
		SELECT:'media_select',
		UNSELECT:'media_unselect',
		UPDATE:'media_update'
	}
}


$.LiquidGear.start();

/*
		private function onLoadControl(e:ModelEvent):void {
			var pageData:xmlData	= e.target as xmlData;
			
			//Cleanup
			pageData.removeEventListener(ModelEvent.LOADED, onLoadControl);
			pageData.removeEventListener(ModelEvent.ERROR, onErrorControl);
			
			_siteXML = new XML(e.params['data']);
			
			//Build Site
			buildSite();
		}
		private function onErrorControl(e:ModelEvent):void {
			var pageData:xmlData	= e.target as xmlData;
			
			//Cleanup
			pageData.removeEventListener(ModelEvent.LOADED, onLoadControl);
			pageData.removeEventListener(ModelEvent.ERROR, onErrorControl);
			
			trace('Error loading: '+_basePath+_xmlName);
		}
		
		//Load background
		private function buildBackground() {
			_background.basePath	= _basePath;
			_background.addEventListener(MediaEvent.LOADED, onLoadBackground);
			_background.addImage(_siteXML.attribute('background'));
			_background.visible		= true;
			_background.alpha		= 1;
			addChildAt(_background, 0);
		}
		
		private function onLoadBackground(e:MediaEvent):void {
			//Cleanup
			_background.removeEventListener(MediaEvent.LOADED, onLoadBackground);
			
			//Make sure size is correct
			onRender(e);
		}
		
		//Load site
		public function buildSite():void {
			trace('Shell::buildSite');
			_siteVars				= {};
			_siteVars['background']	= (_siteXML.attribute('background') != undefined) ? _siteXML.attribute('background') : '';
			_siteVars['preload']	= (_siteXML.attribute('preload') == 'true') ? true : false;
			_siteVars['title']		= _siteXML.attribute('siteTitle');
			_siteVars['url']		= _siteXML.attribute('siteURL');
			_siteVars['load']		= (_siteXML.attribute('load') != undefined) ? _siteVars['load'] : 'first';
			
			//Build Background
			if(_siteXML.attribute('background') != undefined)
				buildBackground();
			
			//Pages
			_pageVars		= {};
			_completeLoaded	= 0;
			_completeTotal	= _siteXML.children().length();
			
			//Set load type
			_loadType		= _siteVars['load'];
				
			//If main preloader is set, load it.
			if(_siteVars['preload'])
				buildPreloader();
			else
				buildPage();
		}
		
		private function buildPreloader():void {
			var preName;
			var preInfo:Object;
			
			//If there is a main preloader, load it.
			if(_siteXML.attribute('preloader')) {
				preName					= String(_siteXML.attribute('preloaderName'));
				_preInfo				= {};
				_preInfo['framed']		= (_siteXML.attribute('framed') != undefined && _siteXML.attribute('framed') == 'true') ? true : false;
				_preInfo['loadName']	= preName + 'Loader';
				_preInfo['pageName']	= preName;
				_preInfo['pageXML']		= 'XML/Preloaders/'+preName+'Loader.xml';
				_preInfo['basePath']	= _basePath;
				
				_preloader	= createPreloader(preName);
			}
		}
		
		private function buildPage():void {
			trace('Shell::buildPage');
			var item:XML		= _siteXML.children()[_pageIdx];
			var pageInfo:Object = {};
			
			pageInfo[String(item.attributes()[_attIdx].name())]	= item.attributes()[_attIdx];
				
			var pageName		= item.attribute('name');
			pageInfo['framed']		= (item.attribute('framed') != undefined && item.attribute('framed') == 'true') ? true : false;
			pageInfo['title']		= item.attribute('title');
			pageInfo['name']		= pageName + 'Page';
			pageInfo['pageName']	= pageName;
			pageInfo['pageXML']		= 'XML/Pages/'+pageName+'Page.xml';
			pageInfo['basePath']	= _basePath;
			
			if(pageInfo['preloader'] != undefined && pageInfo['preloader'] != null && pageInfo['preloader'] == 'true') {
				pageInfo['preloader']	= true;
				
				if(pageInfo['preloaderName'] != undefined && pageInfo['preloaderName'] != null && pageInfo['preloaderName'] != '')
					pageInfo['loadName']	= pageInfo['preloaderName']+'Loader';
				else
					pageInfo['loadName']	= pageName+'Loader';
			} else {
				pageInfo['preloader']	= false;
			}
			
			//Save page data
			_pageVars[pageName] = pageInfo;
			
			//Create Pages
			if(_loadType == 'all') {
				_pages[_pageIdx]	= createPage(pageName);
				_pageIdx++;
				_attIdx++;
			} else {
				//Open first page
				if(_pageIdx == 0) {
					_pages[_pageIdx]	= createPage(pageName);
				}
			}
		}
		
		//Open Page
		public function openPage(pageName, pagePos='front'):void {
			trace('Shell::openPage');
			var page:MovieClip = getPage(pageName);
			trace('page: '+page);
			trace('page.isSetup: '+page.isSetup);
			//Setup Page
			if(!page.isSetup)
				page.setup();
			
			if(!page.isOpen) {
				//Transition page visibility to stage.
				Tween.to(page, _pageVars[pageName]['transition'], {autoAlpha:1});
				
				//Set page order
				if(pagePos == 'front')
					toFront(pageName);
				else
					toBack(pageName);
				
				page.isOpen				= true;
				
				var event:PageEvent		= new PageEvent(PageEvent.OPEN);
				event.params['name']	= pageName;
				page.dispatchEvent(event);
			}
		}
		
		public function closePage(pageName, pageTrans:int=0, pageFrame:Boolean=false):void {
			var page:MovieClip = getPage(pageName);
			
			if(page.isOpen) {
				page.isOpen				= false;
			
				var event:PageEvent		= new PageEvent(PageEvent.CLOSE);
				event.params['name']	= pageName;
				page.dispatchEvent(event);
				
				if(page)
					Tween.to(page, pageTrans, {autoAlpha:0});
			}
		}
		
		//Get Page
		private function getPage(pageName):MovieClip {
			var page:MovieClip;
			
			//Get page if already created
			if(_pageVars[pageName]['framed'] && _frame != null)
				page	= _frame.pageLayer.getChildByName(pageName) as MovieClip;
			else
				page	= getChildByName(pageName) as MovieClip;
			
			//If page is not already created, create it.
			if(page == null)
				page = createPage(pageName);
				
			return page;
		}
		
		//Create Preloader
		private function createPreloader(preName):MovieClip {
			_preInfo['preloader']	= true;
			_preInfo['render']		= false;
			
			var page:MovieClip		= new FlashPages(_preInfo);
			page.name				= _preInfo['pageName'];
			page.alpha				= 1;
			page.visible			= true;
			
			page.addEventListener(PageEvent.SETUP, onLoadPreLoader);
			page.addEventListener(PageEvent.PRELOADED, onPreloaded);
			
			if(_preInfo['framed']) {
				//Add frame to stage
				createFrame();
				
				//Add page to frame
				_frame.pageLayer.addChild(page);
				page.frame	= _frame.pageLayer;
			} else {
				addChild(page);
			}
			
			return page;
		}
		
		//Create Page
		private function createPage(pageName):MovieClip {
			trace('Shell::createPage');
			var page:MovieClip			= new FlashPages(_pageVars[pageName]);
			page.name					= pageName;
			page.alpha					= 0;
			page.visible				= false;
			page.vars['render']			= true;
			page.vars['transition']		= (_pageVars[pageName]['transition'] != undefined) ? _pageVars[pageName]['transition'] : 0;
			page.vars['autoClose']		= (_pageVars[pageName]['autoClose'] != undefined && _pageVars[pageName]['autoClose'] == 'true') ? true : false;
			page.vars['siteTitle']		= _siteVars['title'];
			page.vars['siteURL']		= _siteVars['url'];
			
			//Listen for a setup complete call
			page.addEventListener(PageEvent.SETUP, onPageSetup);
			page.addEventListener(PageEvent.PROGRESS, onPageProgress);
			page.addEventListener(PageEvent.LOADED, onPageLoaded);
			
			//Set design mode
			if(_designMode)
				page.designMode	= true;
			trace('framed: '+_pageVars[pageName]['framed']);
			if(_pageVars[pageName]['framed']) {
				//Add frame to stage
				createFrame();
				
				//Add page to frame
				_frame.pageLayer.addChild(page);
			} else {
				//Add page to stage
				addChild(page);
			}
			
			return page;
		}
		
		//Create Frame
		private function createFrame():void {
			if(_frame == null) {
				//Create frame
				_frame = new FramePage();
				_frame.name = 'frame';
				
				//Add page title to frame
				if(_frame.siteTitle)
					_frame.siteTitle.text	= _siteVars['title'];
				
				//Add frame to stage
				addChild(_frame);
			}
		}
		
		//Page Order
		public function pageToFront(pageName):void {
			var page:MovieClip = getPage(pageName);
			
			if(_pageVars[pageName]['framed'])
				if(_frame.pageLayer.contains(page))
					_frame.pageLayer.setChildIndex(page, _frame.pageLayer.numChildren-1);
			else
				if(contains(page))
					setChildIndex(page, numChildren-1);
		}
		public function pageToBack(pageName):void {
			var page:MovieClip = getPage(pageName);
			
			if(_pageVars[pageName]['framed']) {
				if(_frame.pageLayer.contains(page))
					_frame.pageLayer.setChildIndex(page, 0);
			} else {
				if(contains(page))
					setChildIndex(page, 0);
			}
		}
		
		private function envCheck():void {
			//Set Environment Type
			switch(Capabilities.playerType) {
				case 'PlugIn':
					_development	= false;
					break;
				case 'External':
					_development	= true;
					break;
				default :
					trace('Player Type: ' + Capabilities.playerType);
					break;
			}
			
			//Check for development environment set defaults 
			if(!_development) {
				_basePath	= this.loaderInfo.parameters.basePath;
				_xmlName	= this.loaderInfo.parameters.xmlName;
			}
		}
		
		private function onRender(e:Event):void {
			onResize(e);
		}
		
		private function onResize(e:Event):void {
			if(_background) {
				_background.setSize(stage.stageWidth, stage.stageHeight);
				_background.setPos((_width - stage.stageWidth)/2, 0);
			}
		}
		
		
		//Preloader Actions
		private function onLoadPreLoader(e:PageEvent):void {
			var page:MovieClip	= e.target as MovieClip;
			
			//Cleanup
			page.removeEventListener(PageEvent.SETUP, onLoadPreLoader);
			
			buildPage();
		}
		private function onPreloaded(e:PageEvent):void {
			var page:MovieClip	= e.target as MovieClip;
			
			//Cleanup
			page.removeEventListener(PageEvent.PRELOADED, onPreloaded);
			
			openPage('intro');
		}
		
		
		//Page Actions
		private function onPageProgress(e:PageEvent):void {
			if(_preloader) {
				var percent:Number	= _completePercent + (e.params['percent'] * (1/_completeTotal));
				_preloader.setProgress(percent);
			}
		}
		private function onPageLoaded(e:PageEvent):void {
			trace('onPageLoaded');
			var page:MovieClip	= e.target as MovieClip;
			
			//Cleanup
			page.removeEventListener(PageEvent.LOADED, onPageLoaded);
			page.removeEventListener(PageEvent.PROGRESS, onPageProgress);
			
			_completeLoaded++;
			_completePercent	= _completePercent + (1/_completeTotal)*100;
			
			if((_completeLoaded == _completeTotal) || _loadType == 'first') {
				openPage(page.name);
				//page.setup();
			} else {
				buildPage();
			}
		}
		private function onPageSetup(e:PageEvent):void {
			var page:MovieClip	= e.target as MovieClip;
			
			//Cleanup
			page.removeEventListener(PageEvent.SETUP, onPageSetup);
			
			Tween.to(page, e.params['transition'], {autoAlpha:1});
			
			//Close existing pages
			if(e.params['autoClose'] == 'true') {
				for(var p in _pageVars) {
					if(e.params['name'] != p) {
						closePage(p, .1, _pageVars[p]['preloader']);
					}
				}
			}
			
		}
	}
}
*/